package com.jie.server.modules.system.conreoller;

import com.jie.common.annotation.Log;
import com.jie.common.controller.BaseController;
import com.jie.common.enums.BusinessType;
import com.jie.common.page.TableDataInfo;
import com.jie.common.result.R;
import com.jie.server.common.domain.TreeSelect;
import com.jie.server.modules.system.entity.AreaEntity;
import com.jie.server.modules.system.service.IAreaService;
import com.jie.server.modules.system.vo.AreaExcelVo;
import com.jie.server.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @description: 行政区划 前端控制层
 * @author: xiaojie
 * @since: 2024/12/18 0018
 */
@Api(tags = "行政区划管理")
@RestController
@RequestMapping("/system/area")
public class AreaController extends BaseController {

    @Autowired
    private IAreaService areaService;

    /**
     * 查询省市区表分页
     */
    @PreAuthorize("@customSs.hasPermi('house:area:page')")
    @GetMapping("/page")
    public TableDataInfo page(AreaEntity areaEntity) {
        startPage();
        List<AreaEntity> list = areaService.selectAreaList(areaEntity);
        return getDataTable(list);
    }
    /**
     * 查询省市区表列表
     */
    @PreAuthorize("@customSs.hasPermi('house:area:list')")
    @GetMapping("/list")
    public R list(AreaEntity areaEntity) {
        List<AreaEntity> list = areaService.selectAreaList(areaEntity);
        return R.ok(list);
    }

    /**
     * 获取省市区表详细信息
     */
    @PreAuthorize("@customSs.hasPermi('house:area:query')")
    @GetMapping(value = "/{areaId}")
    public R getInfo(@PathVariable("areaId") Long areaId) {
        return R.ok(areaService.selectAreaByAreaId(areaId));
    }

    /**
     * 新增省市区表
     */
    @PreAuthorize("@customSs.hasPermi('house:area:create')")
    @Log(title = "省市区表", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@RequestBody AreaEntity areaEntity) {
        boolean success = areaService.create(areaEntity);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改省市区表
     */
    @PreAuthorize("@customSs.hasPermi('house:area:edit')")
    @Log(title = "省市区表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{areaId}")
    public R update(@PathVariable Long areaId,  @RequestBody AreaEntity areaEntity) {
        areaEntity.setAreaId(areaId);
        boolean success = areaService.update(areaEntity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除省市区表
     */
    @PreAuthorize("@customSs.hasPermi('house:area:remove')")
    @Log(title = "省市区表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaIds}")
    public R remove(@PathVariable Long[] areaIds) {
        return R.ok(areaService.deleteAreaByAreaIds(areaIds));
    }

    @ApiOperation(value = "获取省一级行政区化列表")
    @GetMapping(value = "/get-province-list")
    public R getProvinceList() {
        return R.ok(areaService.getAreaListByTypeAndParentId(0,null));
    }

    @ApiOperation(value = "获取市/区县行政区化列表")
    @GetMapping(value = "/get-city-list")
    public R getCityOrCountyList(@RequestParam("type") Integer type,
                                 @RequestParam("name") String name,
                                 @RequestParam("parentName") String parentName) {
        return R.ok(areaService.getAreaListByTypeAndParentName(type,name,parentName));
    }


    /**
     * 获取市/区县行政区化下拉树列表
     */
    @ApiOperation("获取市/区县行政区化下拉树列表")
    @GetMapping("/treeselect")
    public R treeselect(AreaEntity areaEntity) {
        List<AreaEntity> list = areaService.selectAreaList(areaEntity);
        List<TreeSelect> tree = areaService.buildAreaTreeSelect(list);
        return R.ok(tree);
    }

    /**
     * 导出省市区表列表
     */
//    @PreAuthorize("@customSs.hasPermi('system:area:export')")
//    @Log(title = "省市区表", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, AreaExcelVo areaEntity) throws IOException {
//        List<AreaExcelVo> list = areaService.list(areaEntity);
//        ExcelUtils.exportExcel(response,"省市区表.xlsx","省市区表",AreaExcelVo.class,list);
//    }

}
